package com.passion.utils;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @program offline-e-commerce-data-warehouse
 * @Description 时间工具类
 * @Author limeihua
 * @Date 2024/10/02 10:41
 * @Version 1.0
 */
public class DateTimeUtil {
    private static final DateTimeFormatter datetimeFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    private static final DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    /**
     * 将时间戳转换为日期类型
     *
     * @param ts
     * @return
     */
    public static String timeStampToDateTime(Long ts) {
        Date dt = new Date(ts);
        LocalDateTime localDateTime = LocalDateTime.ofInstant(dt.toInstant(), ZoneId.systemDefault());
        return datetimeFormat.format(localDateTime);
    }

    /**
     * @param ts
     * @return
     */
    public static String timeStampToDate(Long ts) {
        Date dt = new Date(ts);
        LocalDateTime localDateTime = LocalDateTime.ofInstant(dt.toInstant(), ZoneId.systemDefault());
        return dateFormat.format(localDateTime);
    }

    /**
     * @param datetimeString
     * @return
     */
    public static String datetimeToDate(String datetimeString) {
        return datetimeString.substring(0, 10);
    }

    public static String toTimestamp(String dtStr, boolean isFull) {
        LocalDateTime localDateTime = null;
        if (!isFull) {
            dtStr = dtStr + " 00:00:00";
        }
        localDateTime = LocalDateTime.parse(dtStr, datetimeFormat);
        return String.valueOf(localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli());
    }

    public static String toTimestamp(String dtStr) {
        return toTimestamp(dtStr, true);
    }

}
